<template>
  <div class="app-container">
    <el-row :gutter="20">

      <el-col  :xm="24">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="标题" prop="userName">
            <el-input v-model="queryParams.userName" placeholder="请输入标题" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="流程名称" prop="phoneNumber">
            <el-input v-model="queryParams.phoneNumber" placeholder="请输入流程名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
        
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="search" @click="handleQuery">{{ $t('btn.search') }}</el-button>
            <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="5" class="mb8">
          

          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="launchList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="标识" align="center" key="id" prop="id" v-if="columns.showColumn('id')" />
          <el-table-column prop="title" label="标题" align="center" v-if="columns.showColumn('title')" />
		   <el-table-column prop="group" label="分组" align="center" v-if="columns.showColumn('group')" />
		    <el-table-column prop="description" label="描述" align="center" v-if="columns.showColumn('description')" />
         
          <el-table-column label="创建时间" align="center" prop="creationTime" v-if="columns.showColumn('creationTime')" width="160"></el-table-column>
        <el-table-column label="操作" align="left" width="110" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button text="发起" icon="Edit" @click="launch(scope.row)" > 发起</el-button>
             
            </template>
          </el-table-column>
        </el-table>
        <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>

   
  </div>


   <!-- 发起订单 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="orderRef" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="单据编号" prop="userName">
              <el-input v-model="form.vouchercode" placeholder="请输入单据编号" />
            </el-form-item>
          </el-col>
          
          <el-col :lg="12">
            <el-form-item label="供应商名称">
              <el-select v-model="form.supplierId" placeholder="请选择供应商名称" class="w100">
                <el-option v-for="item in supplierList" :key="item.Id" :label="item.supplierName" :value="item.Id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        <el-col :lg="12">
            <el-form-item label="发货仓库">
              <el-select v-model="form.warehouseId" placeholder="请选择发货仓库" class="w100">
                <el-option v-for="item in warehouseList" :key="item.Id" :label="item.warehouseName" :value="item.Id" >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         <el-col :lg="12">
            <el-form-item label="申请人" prop="phoneNumber">
              <el-input v-model="form.createdBy" placeholder="请输入申请人"  />
            </el-form-item>
          </el-col>         
   
          <el-col :lg="24">
            <el-form-item label="订单状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in OrderStatusOptions" :key="dict.id" :label="dict.dictValue">{{ dict.dictName }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        
        </el-row>
      </el-form>
      <template #footer>
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitOrderWorkflow">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
</template>

<script setup name="user">
import { getToken } from '@/utils/auth'
import { listLaunch } from '@/api/workflow/launch'
import { launchWorkflow } from '@/api/workflow/approval'
import { saveOrder } from '@/api/voucher/order'
import { ConsoleLogger } from '@microsoft/signalr/dist/esm/Utils';

const { proxy } = getCurrentInstance()

const OrderStatusOptions=ref([])
proxy.getDicts('voucher_orderstatus').then((response) => {
  OrderStatusOptions.value = response.result
})

const open = ref(false)
const title =ref([''])
const launchList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const dateRange = ref([])


const supplierList = ref([{'Id':'3a0f594d-8949-c7ea-5339-f691c2398e4e','supplierName':'广州宝迪'},{'Id':'3a0f594e-9e52-6a91-6b26-6105a4f467d9','supplierName':'唯惠集团'}])
const warehouseList = ref([{'Id':'3a0f594d-8949-c7ea-5339-f691c2398e4e','warehouseName':'高值库'},{'Id':'3a0f594e-9e52-6a91-6b26-6105a4f467d9','warehouseName':'低值库'}])

// 列显隐信息
const columns = ref([
  { key: 0, label: `标识`, visible: true, prop: 'id' },
  { key: 1, label: `标题`, visible: true, prop: 'title' },
  { key: 2, label: `分组`, visible: true, prop: 'Group' },
  { key: 3, label: `描述`, visible: true, prop: 'Description' },

  { key: 4, label: `发起时间`, visible: true, prop: 'creationTime' }
])

const data = reactive({
  form: {
    open: false,
    vouchercode: "",
    supplierId: undefined,
    warehouseId:undefined,
    createdBy: "",
    status:0
  },
  queryParams: {
    pageIndex: 0,
    pageSize: 10,
    title: undefined,
    currentStepName: undefined
  }
  
})

const { queryParams, form } = toRefs(data)

/*发起订单工作流 */
function submitOrderWorkflow() { 
  proxy.$refs['orderRef'].validate((valid) => {
    saveOrder(form.value).then((response) => {
      var workflowdata =  {
                    id: '3a04ba90-894d-2e6c-2cd4-2d620aeb25a7',
                    version: 1,
                    inputs: undefined
                }
          launchWorkflow(workflowdata).then((response) => {

      proxy.$modal.msgSuccess('操作成功')
          open.value = false
    }); 
    });
       
    
})

}




/** 查询流程列表 */
function getList() {
  loading.value = true
  debugger;
  listLaunch(proxy.addDateRange(queryParams.value, dateRange.value)).then((res) => {
    
    loading.value = false
    launchList.value = res.result.items
    total.value = res.totalCount
    
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryRef')
  queryParams.value.deptId = undefined
  proxy.$refs.deptTreeRef.setCurrentKey(null)
  handleQuery()
}

function launch(row) {
  debugger;
  open.value = true;
  title.value = "新增";
}
/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 重置操作表单 */
function reset() {
  form.value = {
    title: undefined,
    currentStepName: undefined
  }
  proxy.resetForm('userRef')
}
/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}
/** 新增按钮操作 */

getList()
</script>
<style scoped>
.avatar {
  width: 40px;
}
</style>
@/api/wokflow/launch